import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/mainbox",
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/404.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue"),
  },
  {
    path: "/mainbox",
    name: "mainbox",
    component: () => import("@/views/MainBox.vue"),
  },
  {
    path: "/uploadpic",
    name: "uploadPic",
    component: () => import("@/views/UploadPic.vue"),
  },
  {
    path: "/adduser",
    name: "addUser",
    component: () => import("@/views/AddUser.vue"),
  },
  {
    path: "/edit",
    name: "edit",
    component: () => import("@/views/Edit.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name === "login") {
    next();
  }
  if (!localStorage.getItem("token")) {
    next({
      path: "/login",
    });
  } else {
    next();
  }
});

export default router;
